##################################################################
# /* Author: Gautam Nair and Nicholas Sambanis */
# /* Violence Exposure and Ethnic Identification: Evidence from Kashmir */
# /* Table 2: Summary Statistics */

##################################################################

# change working directory here
# setwd("")

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(dplyr)
library(scales)

##################################################################

rm(list=ls())

library(stargazer)

data.working <- read.csv("nei_kashmir_replication_dataset.csv")

vars <- c(
"age",
"gender_female",
"hhincome_cat_not_5000",
"hhincome_cat_5000_15000",
"hhincome_cat_15000_more",
"education_cat_none",
"education_cat_class6",
"education_cat_class12",
"education_cat_ba",
"main_earner",
"religion_sunni",
"religion_shia",
"religion_sikh",
"religion_hindu"
)
data.subset<- data.working[,vars]

var.mean<- rep(NA, length(vars))
var.sd<- rep(NA, length(vars))

for(i in 1:length(vars)){
	var.mean[i]<- round(mean(data.subset[,i],na.rm=TRUE),2)
	var.sd[i] <- round(sd(data.subset[,i],na.rm=TRUE),2)
}


var.labels <- c(
"Age",
"Gender Female",
"Monthly Household Income <5000 INR (75 USD approx.)",
"Monthly Household Income 5000-15000 INR",
"Monthly Household Income >15000 INR (225 USD approx.)",
"Education No Formal",
"Education Completed 6th Grade",
"Education Completed 12th Grade",
"Education Completed BA",
"Main Earner",
"Religion Sunni Muslim",
"Religion Shia Muslim",
"Religion Sikh",
"Religion Hindu")


table <- as.data.frame(cbind(var.labels, var.mean, var.sd))
names(table) <- c("Variable", "Mean/Proportion", "SD")
table[,2] <- as.character(table[,2])
table[14,2] <- round(mean(data.subset$religion_hindu,na.rm=TRUE),3)
# replace values for Hindu because there are so few and precision is only upto two decimal places
table[,2] <- factor(table[,2])
table

temptype <- c("latex", "text")
tempext <- c(".tex", ".txt")
table.title <- c("Sample Characteristics")
output.file <- c("tf_t_02_sample_characteristics")

for(q in 1:length(temptype)){
	temp.output <- paste(output.file, tempext[q], sep="")
	stargazer(table,
	out=temp.output,
	title= table.title,
	font.size="small",
	summary=FALSE,
	type= temptype[q],
	rownames=FALSE) 
}










